// src/store/slices/settingsSlice.ts
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import AsyncStorage from '@react-native-async-storage/async-storage';

interface SettingsState {
  // 应用设置
  language: string;
  theme: 'light' | 'dark' | 'auto';
  fontSize: 'small' | 'medium' | 'large';

  // 通知设置
  notifications: {
    enabled: boolean;
    chat: boolean;
    translation: boolean;
    voice: boolean;
    subscription: boolean;
    marketing: boolean;
    sound: boolean;
    vibration: boolean;
  };

  // AI设置
  ai: {
    defaultModel: 'gpt-3.5-turbo' | 'gpt-4';
    temperature: number;
    maxTokens: number;
    streamingEnabled: boolean;
    autoSave: boolean;
    historySyncEnabled: boolean;
  };

  // 翻译设置
  translation: {
    autoDetectLanguage: boolean;
    defaultSourceLang: string;
    defaultTargetLang: string;
    saveHistory: boolean;
    showConfidence: boolean;
  };

  // 语音设置
  voice: {
    defaultLanguage: string;
    autoStopRecording: boolean;
    maxRecordingDuration: number;
    playbackSpeed: number;
    voiceType: string;
  };

  // 隐私设置
  privacy: {
    shareAnalytics: boolean;
    personalizedAds: boolean;
    dataCollection: boolean;
    locationTracking: boolean;
  };

  // 存储设置
  storage: {
    cacheEnabled: boolean;
    maxCacheSize: number; // MB
    autoCleanCache: boolean;
    downloadPath: string;
  };

  // 网络设置
  network: {
    wifiOnly: boolean;
    dataSaver: boolean;
    autoRetry: boolean;
    maxRetries: number;
  };
}

const initialState: SettingsState = {
  language: 'zh-CN',
  theme: 'auto',
  fontSize: 'medium',

  notifications: {
    enabled: true,
    chat: true,
    translation: true,
    voice: true,
    subscription: true,
    marketing: false,
    sound: true,
    vibration: true,
  },

  ai: {
    defaultModel: 'gpt-3.5-turbo',
    temperature: 0.7,
    maxTokens: 1000,
    streamingEnabled: true,
    autoSave: true,
    historySyncEnabled: true,
  },

  translation: {
    autoDetectLanguage: true,
    defaultSourceLang: 'auto',
    defaultTargetLang: 'en',
    saveHistory: true,
    showConfidence: false,
  },

  voice: {
    defaultLanguage: 'zh-CN',
    autoStopRecording: true,
    maxRecordingDuration: 60,
    playbackSpeed: 1.0,
    voiceType: 'female',
  },

  privacy: {
    shareAnalytics: true,
    personalizedAds: false,
    dataCollection: true,
    locationTracking: false,
  },

  storage: {
    cacheEnabled: true,
    maxCacheSize: 100,
    autoCleanCache: true,
    downloadPath: '/downloads',
  },

  network: {
    wifiOnly: false,
    dataSaver: false,
    autoRetry: true,
    maxRetries: 3,
  },
};

const settingsSlice = createSlice({
  name: 'settings',
  initialState,
  reducers: {
    updateSettings: (state, action: PayloadAction<Partial<SettingsState>>) => {
      Object.assign(state, action.payload);
      // 保存到本地存储
      AsyncStorage.setItem('settings', JSON.stringify(state));
    },

    setLanguage: (state, action: PayloadAction<string>) => {
      state.language = action.payload;
    },

    setTheme: (state, action: PayloadAction<'light' | 'dark' | 'auto'>) => {
      state.theme = action.payload;
    },

    setFontSize: (state, action: PayloadAction<'small' | 'medium' | 'large'>) => {
      state.fontSize = action.payload;
    },

    updateNotifications: (
      state,
      action: PayloadAction<Partial<SettingsState['notifications']>>
    ) => {
      state.notifications = { ...state.notifications, ...action.payload };
    },

    updateAISettings: (state, action: PayloadAction<Partial<SettingsState['ai']>>) => {
      state.ai = { ...state.ai, ...action.payload };
    },

    updateTranslationSettings: (
      state,
      action: PayloadAction<Partial<SettingsState['translation']>>
    ) => {
      state.translation = { ...state.translation, ...action.payload };
    },

    updateVoiceSettings: (
      state,
      action: PayloadAction<Partial<SettingsState['voice']>>
    ) => {
      state.voice = { ...state.voice, ...action.payload };
    },

    updatePrivacySettings: (
      state,
      action: PayloadAction<Partial<SettingsState['privacy']>>
    ) => {
      state.privacy = { ...state.privacy, ...action.payload };
    },

    updateStorageSettings: (
      state,
      action: PayloadAction<Partial<SettingsState['storage']>>
    ) => {
      state.storage = { ...state.storage, ...action.payload };
    },

    updateNetworkSettings: (
      state,
      action: PayloadAction<Partial<SettingsState['network']>>
    ) => {
      state.network = { ...state.network, ...action.payload };
    },

    resetSettings: () => initialState,

    loadSettingsFromStorage: (state, action: PayloadAction<SettingsState>) => {
      return action.payload;
    },
  },
});

export const {
  updateSettings,
  setLanguage,
  setTheme,
  setFontSize,
  updateNotifications,
  updateAISettings,
  updateTranslationSettings,
  updateVoiceSettings,
  updatePrivacySettings,
  updateStorageSettings,
  updateNetworkSettings,
  resetSettings,
  loadSettingsFromStorage,
} = settingsSlice.actions;

export default settingsSlice.reducer;